const mysql = require('mysql2/promise');

// 数据库配置
const dbConfig = {
    host: '43.143.253.188',
    user: 'nextjs',
    password: 'yEAiRE2jcxTyRBy5',
    database: 'nextjs',
    port: 3306
};

async function checkCasinoTablesStructure() {
    let connection;

    try {
        console.log('🔍 开始检查casino相关表的结构...');

        // 连接数据库
        connection = await mysql.createConnection(dbConfig);
        console.log('✅ 数据库连接成功');

        // 检查casino_dice_bets表结构
        console.log('\n📋 检查casino_dice_bets表结构...');
        const [diceBetsStructure] = await connection.execute('DESCRIBE casino_dice_bets');
        console.log('casino_dice_bets表结构:');
        diceBetsStructure.forEach(field => {
            console.log(`  ${field.Field} (${field.Type}) ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Key ? `KEY: ${field.Key}` : ''}`);
        });

        // 检查casino_chibi_bets表结构
        console.log('\n📋 检查casino_chibi_bets表结构...');
        const [chibiBetsStructure] = await connection.execute('DESCRIBE casino_chibi_bets');
        console.log('casino_chibi_bets表结构:');
        chibiBetsStructure.forEach(field => {
            console.log(`  ${field.Field} (${field.Type}) ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Key ? `KEY: ${field.Key}` : ''}`);
        });

        // 检查casino_rock_paper_scissors表结构
        console.log('\n📋 检查casino_rock_paper_scissors表结构...');
        const [rpsStructure] = await connection.execute('DESCRIBE casino_rock_paper_scissors');
        console.log('casino_rock_paper_scissors表结构:');
        rpsStructure.forEach(field => {
            console.log(`  ${field.Field} (${field.Type}) ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Key ? `KEY: ${field.Key}` : ''}`);
        });

        // 检查casino_dice_rounds表结构
        console.log('\n📋 检查casino_dice_rounds表结构...');
        const [diceRoundsStructure] = await connection.execute('DESCRIBE casino_dice_rounds');
        console.log('casino_dice_rounds表结构:');
        diceRoundsStructure.forEach(field => {
            console.log(`  ${field.Field} (${field.Type}) ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Key ? `KEY: ${field.Key}` : ''}`);
        });

        // 检查casino_chibi_rounds表结构
        console.log('\n📋 检查casino_chibi_rounds表结构...');
        const [chibiRoundsStructure] = await connection.execute('DESCRIBE casino_chibi_rounds');
        console.log('casino_chibi_rounds表结构:');
        chibiRoundsStructure.forEach(field => {
            console.log(`  ${field.Field} (${field.Type}) ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Key ? `KEY: ${field.Key}` : ''}`);
        });

        // 检查casino_rps_rounds表结构
        console.log('\n📋 检查casino_rps_rounds表结构...');
        const [rpsRoundsStructure] = await connection.execute('DESCRIBE casino_rps_rounds');
        console.log('casino_rps_rounds表结构:');
        rpsRoundsStructure.forEach(field => {
            console.log(`  ${field.Field} (${field.Type}) ${field.Null === 'YES' ? 'NULL' : 'NOT NULL'} ${field.Key ? `KEY: ${field.Key}` : ''}`);
        });

        console.log('\n🎉 表结构检查完成！');

    } catch (error) {
        console.error('❌ 检查过程中发生错误:', error.message);
        throw error;
    } finally {
        if (connection) {
            await connection.end();
            console.log('🔌 数据库连接已关闭');
        }
    }
}

// 运行检查脚本
if (require.main === module) {
    checkCasinoTablesStructure()
        .then(() => {
            console.log('✅ 表结构检查脚本执行成功');
            process.exit(0);
        })
        .catch((error) => {
            console.error('💥 表结构检查脚本执行失败:', error.message);
            process.exit(1);
        });
}

module.exports = { checkCasinoTablesStructure };
